package com.spark.zhou.demo.sparksql.sqlcontext;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SQLContext;
import org.apache.spark.sql.SparkSession;

/**
 * @Description: JSON格式数据操作
 * @Author: ZhOu
 * @Date: 2018/5/28
 */
public class JsonData {
    private static final String RES_PATH = JsonData.class.getClass().getResource("/").getPath();
    private static final String JSON_PATH = RES_PATH + "person.json";

    public static void main(String[] args) {
        System.setProperty("HADOOP_USER_NAME", "root");
        SparkConf conf = new SparkConf()
                .setMaster("local")
                .setAppName("P_App");

        JavaSparkContext sc = new JavaSparkContext(conf);
        SQLContext sqlContext = SQLContext.getOrCreate(sc.sc());

        Dataset<Row> people = sqlContext.read().json(JSON_PATH);
        people.printSchema();
        people.show();

        //创建视图，可以使用sql查询
        people.createOrReplaceTempView("people");
        Dataset<Row> sqlData = sqlContext.sql("select id,name,age from people");
        sqlData.show();

        //SparkSession方式
        SparkSession spark = SparkSession.builder()
                .appName("JSON_DATA")
                .getOrCreate();
        Dataset<Row> dataset = spark.read().json(JSON_PATH);
        dataset.show();


    }
}
